// axios({}).then(res => { })
// 需求大小写判断，post传递类型判断

function MyAxios(config){
  const {url,method,params,data} = config
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest()
    if(method.toUpperCase()==='GET'){
      xhr.open('GET',params?url+'?'+setParams(params):url)
      xhr.send()
    }else if(method.toUpperCase()==='POST'){
      xhr.open('POST',url)
      const flag=Object.prototype.toString.call(data)==='[object Object]'
      xhr.setRequestHeader('Content-Type',flag?'applycation/json':'applycation/x-www-form-urlencoded')
      xhr.send(flag?JSON.stringify(data):data)
    }
    xhr.onreadystatechange=function(){
      if(xhr.readyState===4){
        if(xhr.status>=200&&xhr.status<300){
          resolve(xhr.responseText)
        }else{
          reject(xhr.status)
        }
      }
    }
    
  })
}
function setParams(obj){
  let arr=[]
  for(let key in obj){
    arr.push(key+'='+obj[key])
  }
  return arr.join('&')
}
